home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / dig-2.0 / dig-2 / dig.2.0 / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-04  |  4.9 KB  |  202 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13.  
  14. /**************************************************************************
  15.  **                                                                      **
  16.  **  Modified for & distributed with DiG - Version 1.1.beta              **
  17.  **                                                                      **
  18.  **  Distributed with 'dig' version 2.0 release from USC-ISI (9/1/90).   **
  19.  **                                                                      **
  20.  *************************************************************************/
  21.  
  22. #ifndef lint
  23. static char sccsid[] = "@(#)list.c    5.10 (Berkeley) 2/17/88";
  24. #endif /* not lint */
  25.  
  26. #include "hfiles.h"
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include NETDBH
  31. #include <stdio.h>
  32. #include <strings.h>
  33. #include <ctype.h>
  34. #include NAMESERH
  35. #include RESOLVH
  36. #include "res.h"
  37.  
  38. /*
  39.  *  Imported from res_debug.c
  40.  */
  41. extern char *_res_resultcodes[];
  42.  
  43. typedef union {
  44.     HEADER qb1;
  45.     char qb2[PACKETSZ];
  46. } querybuf;
  47.  
  48. extern u_long         inet_addr();
  49. extern HostInfo     *defaultPtr;
  50. extern HostInfo     curHostInfo;
  51. extern int         curHostValid;
  52. extern int              sockFD;
  53.  
  54. #define HASH_SIZE 50
  55.  
  56. int
  57. do_zone(domain,qtype)
  58.      char *domain;
  59.      int  qtype;
  60. {
  61.     querybuf         buf;
  62.     struct sockaddr_in     sin;
  63.     HEADER             *headerPtr;
  64.     int             msglen;
  65.     int             amtToRead;
  66.     int             numRead;
  67.     int             soacnt = 0;
  68.     u_short         len;
  69.     char             *cp, *nmp;
  70.     char             dname[2][NAME_LEN];
  71.     FILE*                   filePtr;
  72.     enum {
  73.         NO_ERRORS, 
  74.         ERR_READING_LEN, 
  75.         ERR_READING_MSG,
  76.         ERR_PRINTING,
  77.     } error = NO_ERRORS;
  78.  
  79.     msglen = res_mkquery(QUERY, domain, C_IN, T_AXFR,
  80.                 (char *)0, 0, (char *)0, 
  81.                 (char *) &buf, sizeof(buf));
  82.     if (msglen < 0) {
  83.         if (_res.options & RES_DEBUG) {
  84.           (void) fprintf(stderr,
  85.                  "; ***zone transfer: res_mkquery failed\n");
  86.         }
  87.         return (ERROR);
  88.     }
  89.  
  90.     bzero((char *)&sin, sizeof(sin));
  91.     sin.sin_family    = AF_INET;
  92.     sin.sin_port    = _res.nsaddr.sin_port;
  93.  
  94.     sin.sin_addr =  _res.nsaddr.sin_addr; /* .s_addr; */
  95.  
  96.     if ((sockFD = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  97.         perror("ListHosts");
  98.         return(ERROR);
  99.     }
  100.     if (connect(sockFD, &sin, sizeof(sin)) < 0) {
  101.         perror("ListHosts");
  102.         (void) close(sockFD);
  103.         sockFD = -1;
  104.         return(ERROR);
  105.     }    
  106.  
  107.         len = htons(msglen);
  108.  
  109.         if (write(sockFD, (char *)&len, sizeof(len)) != sizeof(len) ||
  110.             write(sockFD, (char *) &buf, msglen) != msglen) {
  111.         perror("ListHosts");
  112.         (void) close(sockFD);
  113.         sockFD = -1;
  114.         return(ERROR);
  115.     }
  116.  
  117.     filePtr = stdout;
  118.  
  119.     while (1) {
  120.  
  121.         cp = (char *) &buf;
  122.         amtToRead = sizeof(u_short);
  123.         while((amtToRead > 0) && 
  124.           ((numRead = read(sockFD, cp, amtToRead)) > 0)) {
  125.         cp       += numRead;
  126.         amtToRead -= numRead;
  127.         }
  128.         if (numRead <= 0) {
  129.         error = ERR_READING_LEN;
  130.         break;
  131.         }    
  132.  
  133.         if ((len = htons(*(u_short *)&buf)) == 0) {
  134.         break;    /* nothing left to read */
  135.         }
  136.  
  137.         amtToRead = len;
  138.         cp = (char *) &buf;
  139.         while((amtToRead > 0) &&
  140.           ((numRead = read(sockFD, cp, amtToRead)) > 0)) {
  141.         cp += numRead;
  142.         amtToRead -= numRead;
  143.         }
  144.         if (numRead <= 0) {
  145.         error = ERR_READING_MSG;
  146.         break;
  147.         }
  148.  
  149.         cp = buf.qb2 + sizeof(HEADER);
  150.         if (ntohs(buf.qb1.qdcount) > 0)
  151.         cp += dn_skipname(cp, buf.qb2 + len) + QFIXEDSZ;
  152.         nmp = cp;
  153.         cp += dn_skipname(cp, (u_char *)&buf + len);
  154.  
  155.         if ((_getshort(cp) == T_SOA)) {
  156.         dn_expand(buf.qb2, buf.qb2 + len, nmp, dname[soacnt],
  157.             sizeof(dname[0]));
  158.             if (soacnt) {
  159.             if (strcmp(dname[0], dname[1]) == 0) {
  160.               (void) printf(";  Matching SOA found\n");
  161.               break;
  162.             }
  163.           } else {
  164.             fp_query((char *) &buf, filePtr);
  165.             soacnt++;
  166.           }
  167.           } else
  168.         fp_query((char *) &buf, filePtr);
  169.  
  170.         headerPtr = (HEADER *) &buf;
  171.         if (headerPtr->rcode != NOERROR) break;
  172.       }
  173.  
  174.     (void) close(sockFD);
  175.     sockFD = -1;
  176.  
  177.     switch (error) {
  178.     case NO_ERRORS:
  179.       return (SUCCESS);
  180.     case ERR_READING_LEN:
  181.       return(ERROR);
  182.     case ERR_PRINTING:
  183.       (void) fprintf(stderr,
  184.              "; *** Error during listing of %s:\n",    domain);
  185.       return(ERROR);
  186.     case ERR_READING_MSG:
  187.       headerPtr = (HEADER *) &buf;
  188.       (void) fprintf(stderr,
  189.              "; ListHosts: error receiving zone transfer:\n");
  190.       (void) fprintf(stderr,
  191.           "; result: %s, answers = %d, authority = %d, additional = %d\n",
  192.           _res_resultcodes[headerPtr->rcode], 
  193.           ntohs(headerPtr->ancount), ntohs(headerPtr->nscount), 
  194.           ntohs(headerPtr->arcount));
  195.       return(ERROR);
  196.     default:
  197.       return(ERROR);
  198.     }
  199.       }
  200.  
  201.  
  202.